package com.wxproject.demo.Utils;/*
 *项目名: WXTEST
 *文件名: LoginCountUtil
 *创建者: ZQF
 *创建时间:2021/1/14 17:19
 *描述: TODO
 */

import com.wxproject.demo.Entity.LoginEntity;
import com.wxproject.demo.Entity.VO.AllUserInfo;
import com.wxproject.demo.Service.LoginService;
import com.wxproject.demo.Service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Configuration
public class LoginCountUtil {
    @Autowired
    private LoginService loginService;
    @Autowired
    private UserInfoService userInfoService;


    public Map<Object, Object> LoginCountCheck(LoginEntity userEntity,String realPassword){
        String secret = "UserLoginCount-"+userEntity.getUsername();
        Integer count = (Integer) RedisUtil.get(secret);
        Map<Object,Object> map = new HashMap<>();
        Integer ban = loginService.getUserBanId(userEntity.getUsername());
        if (ban!=null&&ban==2){
            map.put("code",401);
            map.put("message","请先激活账户");
            return map;
        }

        if (count != null && count == 5) {
            map.put("code",401);
            map.put("message","请十分钟后重试");
            return map;
        }
        if (realPassword == null ) {
            map.put("code",401);
            map.put("message","该账户未注册");
            return map;
        } else if (!realPassword.equals(MD5Utils.code(userEntity.getPassword())))  {
            if (count == null) {
                RedisUtil.set(secret, 1, 60 * 10);
                map.put("code",401);
                map.put("message","密码错误,你还有4次登陆机会");
                return map;
            }
            if (count < 5) {
                RedisUtil.incr(secret, 1);
                if (4 - count == 0) {
                    map.put("code",401);
                    map.put("message","请十分钟后重试");
                    return map;
                }
                map.put("code",401);
                map.put("message","密码错误，你还有" + (4 - count) + "次登陆机会");
                return map;
            }
        }
        String jwtId = UUID.randomUUID().toString();
        String userId = loginService.getUserIdByUsername(userEntity.getUsername());
        String token = JWTUtil.createToken(jwtId,userId,userEntity.getUsername());
        String freshToken = JWTUtil.createToken(jwtId,userId,userEntity.getUsername());
        Map<String,Object> map1 = new HashMap<>();
        map1.put("access_token",token);
        map1.put("fresh_token",freshToken);
        RedisUtil.del(secret);

        //2021/4/15
        RedisUtil.set(userId,new Date(),2592000);
        AllUserInfo allUserInfo = userInfoService.getAllUserInfo(JWTUtil.getUserId(freshToken));
        Map<String, Object> infoMap = new HashMap<>();
        infoMap.put("id",allUserInfo.getId());
        infoMap.put("nickname",allUserInfo.getNickname());
        infoMap.put("avatarUrl",allUserInfo.getAvatarUrl());
        RedisUtil.hmset("userInfo-"+allUserInfo.getUsername(),infoMap);
        map.put("message","登陆成功");
        map.put("code",200);
        map.put("userId",userId);
        map.put("Token",token);
        map.put("freshToken",freshToken);
        return map;
    }
}
